package com.wingscode.modules.sys.controller;

import com.wingscode.common.annotation.SysLog;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.common.validator.ValidatorUtils;
import com.wingscode.common.validator.group.AddGroup;
import com.wingscode.common.validator.group.UpdateGroup;
import com.wingscode.modules.sys.entity.RegionEntity;
import com.wingscode.modules.sys.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 区域
 *
 * @author yan
 */
@RestController
@RequestMapping("/region")
public class RegionController extends AbstractController {
	@Autowired
	private RegionService regionService;

	
	/**
	 * 父级列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = regionService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 子级列表
	 */
	@GetMapping("/childList")
	public R childList(@RequestParam Map<String, Object> params){
		if (!params.containsKey("pid"))
			return R.error("pid为空");

		PageUtils page = regionService.queryChildPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存区域")
	@PostMapping("/save")
	public R save(@RequestBody RegionEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);

		regionService.save(entity);

		return R.ok();
	}

	/**
	 * 修改区域
	 */
	@SysLog("修改区域")
	@PostMapping("/update")
	public R update(@RequestBody RegionEntity entity){
		ValidatorUtils.validateEntity(entity, UpdateGroup.class);

		regionService.updateById(entity);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除区域")
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] Ids){
		regionService.delete(Ids);

		return R.ok();
	}
}
